Coverage Report

Created: 2024-12-26 12:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\compiler\src\gen\swift\core.rs
Line
Count
Source
1
// Copyright (c) 2024, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
use crate::api::core::loader::Options;
30
use crate::codec_map_initializer;
31
use crate::compiler::Protocol;
32
use crate::compiler::util::protocols::ProtocolStore;
33
use crate::gen::base::Error;
34
use crate::gen::codec::CodecMap;
35
use crate::gen::file::{Content, File, FileType};
36
use crate::gen::swift::imports::gen_imports;
37
use crate::gen::swift::message::{gen_message_decl, gen_message_from_slice_impl, gen_message_write_impl};
38
use crate::gen::swift::r#enum::gen_enum_decl;
39
use crate::gen::swift::solver::SwiftImportSolver;
40
use crate::gen::swift::structure::gen_structure_decl;
41
use crate::gen::swift::union::gen_union_decl;
42
use crate::gen::template::Template;
43
use crate::gen::Generator;
44
45
const TEMPLATE_CODEC_BASE: &[u8] = include_bytes!("./default_codec/base.template");
46
const TEMPLATE_CODEC_STRING: &[u8] = include_bytes!("./default_codec/string.template");
47
const TEMPLATE_CODEC_LIST: &[u8] = include_bytes!("./default_codec/list.template");
48
49
pub struct GeneratorSwift;
50
51
impl Generator for GeneratorSwift {
52
    type Error = Error;
53
    type Params<'a> = ProtocolStore<'a, SwiftImportSolver, Options<'a>>;
54
55
0
    fn get_default_codecs<'fragment, 'variable>() -> CodecMap<'fragment, 'variable> {
56
0
        let mut codecs = CodecMap::new(codec_map_initializer! {
57
0
            "base" => TEMPLATE_CODEC_BASE
58
0
        });
59
0
        codecs.insert(
60
0
            "string",
61
0
            Template::compile_with_includes(TEMPLATE_CODEC_STRING, &codecs).unwrap(),
62
0
        );
63
0
        codecs.insert(
64
0
            "list",
65
0
            Template::compile_with_includes(TEMPLATE_CODEC_LIST, &codecs).unwrap(),
66
0
        );
67
0
        codecs
68
0
    }
69
70
0
    fn generate(
71
0
        proto: &Protocol,
72
0
        codec_map: &CodecMap,
73
0
        params: &ProtocolStore<SwiftImportSolver, Options>,
74
0
    ) -> Result<Vec<File>, Self::Error> {
75
0
        let imports = gen_imports(params);
76
0
        let decl_structures = proto.structs.iter().map(|v| gen_structure_decl(proto, v));
77
0
        let decl_enums = proto.enums.iter().map(|v| gen_enum_decl(proto, v));
78
0
        let decl_messages_code = proto.messages.iter().map(|v| gen_message_decl(proto, codec_map, v));
79
0
        let impl_from_slice_messages_code =
80
0
            proto.messages.iter().map(|v| gen_message_from_slice_impl(proto, codec_map, v));
81
0
        let impl_write_messages_code = proto.messages.iter().map(|v| gen_message_write_impl(proto, codec_map, v));
82
0
        let decl_unions = proto.unions.iter().map(|v| gen_union_decl(proto, v));
83
0
        Ok(vec![
84
0
            File::new(
85
0
                FileType::Structure,
86
0
                format!("{}.structures.swift", proto.name()),
87
0
                Content::from_iter(decl_structures).header(&imports),
88
0
            ),
89
0
            File::new(
90
0
                FileType::Enum,
91
0
                format!("{}.enums.swift", proto.name()),
92
0
                &Content::from_iter(decl_enums),
93
0
            ),
94
0
            File::new(
95
0
                FileType::Message,
96
0
                format!("{}.messages.swift", proto.name()),
97
0
                Content::try_from_iter(decl_messages_code)?.header(&imports),
98
0
            ),
99
0
            File::new(
100
0
                FileType::MessageWriting,
101
0
                format!("{}.messages_write.swift", proto.name()),
102
0
                Content::try_from_iter(impl_write_messages_code)?.header(&imports),
103
0
            ),
104
0
            File::new(
105
0
                FileType::MessageReading,
106
0
                format!("{}.messages_from_bytes.swift", proto.name()),
107
0
                Content::try_from_iter(impl_from_slice_messages_code)?.header(&imports),
108
0
            ),
109
0
            File::new(
110
0
                FileType::Union,
111
0
                format!("{}.unions.swift", proto.name()),
112
0
                Content::from_iter(decl_unions).header(&imports),
113
0
            ),
114
        ])
115
0
    }
116
117
0
    fn get_language_extension() -> &'static str {
118
0
        "swift"
119
0
    }
120
}